﻿using System.ComponentModel;
using Windows.UI;
using Windows.UI.ApplicationSettings;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace wBudget
{
    public sealed partial class ApplicationSettings : INotifyPropertyChanged
    {
        public delegate void ApplicationSettingsChangedEventHandler();
        public static event ApplicationSettingsChangedEventHandler ApplicationSettingsChanged;

        public event PropertyChangedEventHandler PropertyChanged;

        public bool UseCustomCurrency
        {
            get { return DataManager.UseCustomCurrency; }
            set 
            {
                DataManager.UseCustomCurrency = value;
                PropertyChanged(this, new PropertyChangedEventArgs("UseCustomCurrency"));
                if (ApplicationSettingsChanged != null)
                    ApplicationSettingsChanged();
            }
        }

        public string Currency
        {
            get { return DataManager.CurrencyString; }
            set 
            { 
                DataManager.CurrencyString = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Currency"));
                if (ApplicationSettingsChanged != null)
                    ApplicationSettingsChanged();
            }
        }

        public ApplicationSettings()
        {
            InitializeComponent();
            DataContext = this;
        }

        public static void ShowSettings() {
            var popup = new Popup { Width = 346, Height = Window.Current.Bounds.Height, IsLightDismissEnabled = true };

            var settings = new ApplicationSettings { Width = popup.Width, Height = popup.Height };

            popup.Child = new Border { Child = settings, Background = new SolidColorBrush(Colors.LightGray) };
            popup.SetValue(Canvas.LeftProperty, Window.Current.Bounds.Width - popup.Width);
            popup.SetValue(Canvas.TopProperty, 0);

            popup.IsOpen = true;
        }

        private void BackClicked(object sender, RoutedEventArgs e)
        {
            var parent = Parent as Popup;
            if (parent != null)
            {
                parent.IsOpen = false;
            }

            // If the app is not snapped, then the back button shows the Settings pane again.
            if (Windows.UI.ViewManagement.ApplicationView.Value != Windows.UI.ViewManagement.ApplicationViewState.Snapped)
            {
                SettingsPane.Show();
            }
        }
    }
}
